<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">











<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>testnguice - Injection and TestCases</title>
    <style type="text/css" media="all">
      @import url("./css/maven-base.css");
      @import url("./css/maven-theme.css");
      @import url("./css/site.css");
    </style>
    <link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
          <meta name="author" content="Simone Tripodi" />
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
      </head>
  <body class="composite">
    <div id="banner">
                    <span id="bannerLeft">
    
            TestNGuice
    
            </span>
                        <a href="http://www.asemantics.com/" id="bannerRight">
    
                                    <img src="http://www.asemantics.com/images/logo.gif" alt="" />
    
            </a>
            <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="breadcrumbs">
          
  

  
    
  
  
    
            <div class="xleft">
        Last Published: 2009-03-22
                          |   
          Version: 1.1
                        </div>
            <div class="xright">            <a href="../testnguice/">Project Page</a>
            |
                <a href="../testnguice/downloads/list">Downloads</a>
            |
                <a href="http://testng.org/" class="externalLink">TestNG</a>
            |
                <a href="http://testng.org/doc/book.html" class="externalLink">TEH TestNG Book</a>
            |
                <a href="../google-guice/">Google Guice</a>
            |
                <a href="http://www.antlr.org/" class="externalLink">ANTLR</a>
            
  

  
    
  
  
    
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="leftColumn">
      <div id="navcolumn">
           
  

  
    
  
  
    
                   <h5>Injectlet</h5>
            <ul>
              
    <li class="none">
                    <a href="index.html">Introduction</a>
          </li>
              
          
              
            
            
      
              
            <li class="expanded">
                    <a href="">User's Guide</a>
                  <ul>
                  
    <li class="none">
              <strong>Injection and TestCases</strong>
        </li>
                  
    <li class="none">
                    <a href="providers.html">Data Providers</a>
          </li>
              </ul>
        </li>
          </ul>
              <h5>Project Documentation</h5>
            <ul>
              
                
              
      
            
      
            
      
            
      
            
      
            
      
            
      
            
      
              
        <li class="collapsed">
                    <a href="project-info.html">Project Information</a>
                </li>
              
                
              
      
            
      
            
      
            
      
            
      
            
      
            
      
            
      
            
      
            
      
            
      
            
      
              
        <li class="collapsed">
                    <a href="project-reports.html">Project Reports</a>
                </li>
          </ul>
                                           <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
            <img alt="Built by Maven" src="./images/logos/maven-feather.png"></img>
          </a>
                       
  

  
    
  
  
    
        </div>
    </div>
    <div id="bodyColumn">
      <div id="contentBox">
        <div class="section"><h2><a name="Injection_and_TestCases"></a>
Injection and TestCases</h2>

            Very easy - but only if you already know both TestNG and google-guice!!!<br />
<p>Step 1: define your business objects
                <div class="source"><pre>interface Service {

    void go();

}

class ServiceImpl implements Service {

    public void go() {
        System.out.println(&quot;This is a test message&quot;);
    }

}</pre>
</div>
</p>
<p>Step 2: define now the TestNG Object Factory
                <div class="source"><pre>class ServiceTestObjectFactory extends com.asemantics.testnguice.inject.AbstractTestObjectFactory {

    public ServiceTestObjectFactory() {
        super(new Module[] { new Module() {
            public void configure(Binder binder) {
                binder.bind(Service.class).to(ServiceImpl.class);
            }
        }});
    }

}</pre>
</div>
</p>
<p>Step 3: define your test case(s)
                <div class="source"><pre>class ServiceTestCase {

    @com.google.inject.Inject
    private Service service;

    public void setService(Service service) {
        this.service = service;
    }

    @org.testng.annotations.Test
    public void verifyExpectedMessage() {
        assert &quot;This is a test message&quot;.equals(service.getMessage());
    }

}</pre>
</div>
</p>
<p>Step 4: configure <code>testng.xml</code> in way to specify the Object Factory
                <div class="source"><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot;&gt;
&lt;suite name=&quot;my-test-suite&quot;&gt;

    &lt;test name=&quot;my-test&quot;&gt;
        &lt;classes&gt;
            &lt;class name=&quot;ServiceTestObjectFactory&quot;/&gt;
            &lt;class name=&quot;ServiceTestCase&quot;/&gt;
        &lt;/classes&gt;
    &lt;/test&gt;

&lt;/suite&gt;</pre>
</div>

                Don't forget to specify the Object Factory <b>before</b>
 the rest of test classes!!!
            </p>
<p>Final step: Run TestNG and enjoy :)</p>
</div>

      </div>
    </div>
    <div class="clear">
      <hr/>
    </div>
    <div id="footer">
      <div class="xright">&#169;  
          2009
    
          Asemantics S.r.l.
          
  

  
    
  
  
    
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
  </body>
</html>
